home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / tcisam.zip / ISTART.C < prev    next >
Text File  |  1987-08-21  |  3KB  |  90 lines

  1. /*
  2.  * ISTART.C - setup for sequential I/O
  3.  *
  4.  *                      Copyright (c) 1987, Jim Mischel
  5.  * Modifications:
  6.  *
  7.  * 08/13/87 - jim - original coding
  8.  */
  9.  
  10. #include "inxdefs.h"
  11.  
  12. /*
  13.  * istart() - Initialize file for sequential read.  Returns 0 if successful,
  14.  * EOF if unsuccessful.  No data is transferred.
  15.  */
  16. int istart(void *d, char cond, void *src)
  17. {
  18.   register df_rec *db_control = (df_rec *)d;
  19.   char *source = (char *)src;
  20.  
  21.   db_control->df_flags |= (DF_EOF + DF_TOF);    /* if start fails, EOF is signaled */
  22.  
  23.   if (cond == START_FILE) {
  24.     /*
  25.      * Start at the beginning of the file.  Get the root, then traverse left
  26.      * nodes until left node is a thread pointer.  If the root is non-existent,
  27.      * return EOF.
  28.      */
  29.     if (iget_root(db_control))
  30.       return(EOF);                      /* couldn't get the root */
  31.  
  32.     /* found the root, now travel down the left nodes */
  33.     while (!(db_control->df_inx_buff.if_flags & LTHRD))
  34.       if (iread_inx(db_control,db_control->df_inx_buff.if_left_node))
  35.         return(EOF);
  36.   }
  37.   else if (cond == END_FILE) {
  38.     /*
  39.      * Start at the end of the file.  Get the root, then traverse right
  40.      * nodes until right node is a thread pointer.  If the root is
  41.      * non-existent, return EOF.
  42.      */
  43.     if (iget_root(db_control))
  44.       return(EOF);                      /* must be empty file */
  45.  
  46.     /* found the root, now travel down the right nodes */
  47.     while (!(db_control->df_inx_buff.if_flags & RTHRD))
  48.       if (iread_inx(db_control,db_control->df_inx_buff.if_right_node))
  49.         return(EOF);
  50.   }
  51.   else {
  52.     /* search for the key */
  53.     switch (isearch(db_control,(source+db_control->df_key_offset))) {
  54.       case -1   :                       /* supplied key < last record read */
  55.         if (cond == GE || cond == GT) {
  56.           if (iget_next(db_control,&db_control->df_inx_buff))
  57.             return(EOF);
  58.         }
  59.         else if (cond == EQ)
  60.           return(EOF);
  61.         break;
  62.       case 0    :                       /* supplied key == last record read */
  63.         if (cond == GT) {
  64.           if (iget_next(db_control,&db_control->df_inx_buff))
  65.             return(EOF);
  66.         }
  67.         else if (cond == LT)
  68.           if (iget_prev(db_control,&db_control->df_inx_buff))
  69.             return(EOF);
  70.         break;
  71.       case 1    :                       /* supplied key > last record read */
  72.         if (cond == LE || cond == LT) {
  73.           if (iget_prev(db_control,&db_control->df_inx_buff))
  74.             return(EOF);
  75.         }
  76.         else if (cond == EQ)
  77.           return(EOF);
  78.         break;
  79.       default   : return(EOF);          /* some error occured during search */
  80.     } /* switch */
  81.   } /* else */
  82.  
  83.   db_control->df_flags &= ~(DF_EOF | DF_TOF);
  84.   db_control->df_flags |= DF_START;
  85.  
  86.   /* copy index buffer into next buffer for readnext operations */
  87.   memcpy(&db_control->df_nxt_buff,&db_control->df_inx_buff,sizeof(inx_rec));
  88.   return(0);
  89. } /* istart */
  90.